package com.shanghaionstar.smart.presenter;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.framework.mvp.base.core.PermissionCall;
import com.onstar.cn.weight.ToastView;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.smart.SmartConnectingFrg;
import com.shanghaionstar.smart.SmartHomeActivity;
import com.shanghaionstar.smart.SmartIntroduceFrg;
import com.shanghaionstar.smart.SmartNoFoundDeviceFrg;
import com.shanghaionstar.smart.adapter.ConfigureArg;
import com.shanghaionstar.smart.iview.ISmartAddDeviceView;
import com.shanghaionstar.smart.util.NetUtil;
import com.shanghaionstar.smart.zxing.MipcaActivityCapture;
import com.shanghaionstar.utils.BusinessUtil;

/**
 * Created by 7828wc on 2016/6/22.
 */
public class SmartAddDevicePresenter extends BasePresenter<Object> {
    private BaseActivity mContext;
    private ISmartAddDeviceView mSmartAddDeviceView;


    public SmartAddDevicePresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<Object> view) {
        super.setView(view);
        mSmartAddDeviceView = (ISmartAddDeviceView) view;
        mContext = (BaseActivity) mSmartAddDeviceView.onActivity();
    }

    public void goToWifiSetting() {
        Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
        mContext.startActivity(intent);
    }

    public void goToFunIntro() {
        SmartIntroduceFrg introFrg = new SmartIntroduceFrg();
        mContext.changeFragment(mContext.getSupportFragmentManager(), introFrg, SmartIntroduceFrg.class, true);
    }

    public void goToConnect(String mQRcode, String routerSsid, String routerPw) {
        if (TextUtils.isEmpty(mQRcode))
            return;
        String ssid = NetUtil.getMediaSsid(mQRcode, mContext);
        if (NetUtil.containSsid(ssid, mContext)) {
            goToConnecting(mQRcode, routerSsid, routerPw);
        } else {
            goToNoFoundDevice();
        }
    }


    private void goToConnecting(String mQRcode, String routerSsid, String routerPw) {
        SmartConnectingFrg connectionFrg = new SmartConnectingFrg();
        Bundle bun = new Bundle();
        ConfigureArg arg = new ConfigureArg(mQRcode, routerSsid, routerPw);
        bun.putSerializable("arg", arg);
        connectionFrg.setArguments(bun);
        mContext.changeFragment(mContext.getSupportFragmentManager(), connectionFrg, SmartConnectingFrg.class, true);
    }

    private void goToNoFoundDevice() {
        BusinessUtil.sendReportByClickType(mContext, FunctionNameFindId.Smarthome_nofindappwifi.getStringValue());
        SmartNoFoundDeviceFrg nfDeviceFrg = new SmartNoFoundDeviceFrg();
        mContext.changeFragment(mContext.getSupportFragmentManager(), nfDeviceFrg, SmartNoFoundDeviceFrg.class, true);
    }

    public void goToCapture(String routerSsid, String routerPw) {
        if (TextUtils.isEmpty(routerSsid)) {
            new ToastView(mContext).showToast(mContext.getString(R.string.smart_wifi_name_empty));
            return;
        }
        if (TextUtils.isEmpty(routerPw)) {
            new ToastView(mContext).showToast(mContext.getString(R.string.smart_wifi_pw_empty));
            return;
        }
        mContext.requestRunTimePermissions(new String[]{Manifest.permission.CAMERA}, new PermissionCall() {
            @Override
            public void requestSuccess() {
                Intent intent = new Intent(mContext, MipcaActivityCapture.class);
                intent.putExtra("requestCode", SmartHomeActivity.REQUEST_CODE);
                mContext.startActivityForResult(intent, SmartHomeActivity.REQUEST_CODE);

            }

            @Override
            public void refused() {

            }
        });
    }

}



